package com.aletto.general_store.utils.watchers;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class TextViewObserver implements Observer, TextWatcher, CompoundButton.OnCheckedChangeListener {

    private IRule rule;
    private Subject subject;

    public TextViewObserver(TextView textView) {
        textView.addTextChangedListener(this);
    }

    public TextViewObserver(CheckBox checkBox) {
        checkBox.setOnCheckedChangeListener(this);
    }

    @Override
    public void bind(Subject subject, IRule rule) {
        this.rule = rule;
        this.subject = subject;
    }

    @Override
    public void update() {
        // 不使用
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        subject.validation(this, rule.validation());
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        subject.validation(this, rule.validation());
    }

}
